package com.example.multimediademo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    ArrayAdapter<String> adapter;
    List<String> contactsList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contact_layout);
        ListView contactsView = (ListView) findViewById(R.id.list1);
        adapter = new ArrayAdapter<String>(this, android.R.layout.
                simple_list_item_1,contactsList);
        contactsView.setAdapter(adapter);
        if (ContextCompat.checkSelfPermission(this, Manifest.permission
                .READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{
                    Manifest.permission.READ_CONTACTS},1);
        }else{
            readContacts();
        }
            contactsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    String result = parent.getItemAtPosition(position).toString();//获取选择项的值
                    Toast.makeText(MainActivity.this,"您点击了"+result,Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(MainActivity.this, SendActivity.class);
                    intent.putExtra("name_phone",result);
                    startActivity(intent);
                }
            });
    }

    private void readContacts() {
        Cursor cursor = null;
        try{
            //获取数据
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds
            .Phone.CONTENT_URI,null,null,null,null);
            if(cursor !=null){
                while (cursor.moveToNext()){
                    //获取联系人姓名
                    @SuppressLint("Range") String displayName =cursor.getString(cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    //获取联系人电话
                    @SuppressLint("Range") String number =cursor.getString(cursor.getColumnIndex
                            (ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactsList.add(displayName+"\n"+number);
                }
                adapter.notifyDataSetChanged();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            {
                if (cursor!=null){
                    cursor.close();
                }
            }
        }
    }
    @SuppressLint("MissingSuperCall")
    @Override
    public void onRequestPermissionsResult(int requestCode,String[] permissions,
                                           int[] grantResults) {
        switch (requestCode) {
            case 1:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    readContacts();
                } else {
                    Toast.makeText(this, "未找到联系人", Toast.LENGTH_SHORT).show();
                }
                break;
            default:
        }
    }
}